package com.ruoyi.web.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TaskUserMark;
import com.ruoyi.system.service.ITaskUserMarkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 测评任务打分Controller
 * 
 * @author ruoyi
 * @date 2024-05-19
 */
@RestController
@RequestMapping("/system/mark")
public class TaskUserMarkController extends BaseController
{
    @Autowired
    private ITaskUserMarkService taskUserMarkService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询测评任务打分列表
     */
    @PreAuthorize("@ss.hasPermi('system:mark:list')")
    @GetMapping("/list")
    public TableDataInfo list(TaskUserMark taskUserMark)
    {
        startPage();
        List<TaskUserMark> list = taskUserMarkService.selectTaskUserMarkList(taskUserMark);
        return getDataTable(list);
    }

    @GetMapping(value = "/preposition")
    public AjaxResult preposition() {
        Long userId = SecurityUtils.getUserId();
        // 用来倒计是5秒的 如果他是null 那么就是说明看过了
        stringRedisTemplate.opsForValue().set(String.valueOf(userId), String.valueOf(userId), 5, TimeUnit.SECONDS);
        // 用来判断有没有查看的
        stringRedisTemplate.opsForValue().set(userId + "EXIT", String.valueOf(userId), 10, TimeUnit.MINUTES);
        return AjaxResult.success();
    }

    /**
     * 导出测评任务打分列表
     */
    @PreAuthorize("@ss.hasPermi('system:mark:export')")
    @Log(title = "测评任务打分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskUserMark taskUserMark)
    {
        List<TaskUserMark> list = taskUserMarkService.selectTaskUserMarkList(taskUserMark);
        ExcelUtil<TaskUserMark> util = new ExcelUtil<TaskUserMark>(TaskUserMark.class);
        util.exportExcel(response, list, "测评任务打分数据");
    }

    /**
     * 获取测评任务打分详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:mark:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(taskUserMarkService.selectTaskUserMarkById(id));
    }

    /**
     * 新增测评任务打分
     */
    @PreAuthorize("@ss.hasPermi('system:mark:add')")
    @Log(title = "测评任务打分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskUserMark taskUserMark)
    {
        return toAjax(taskUserMarkService.insertTaskUserMark(taskUserMark));
    }

    /**
     * 修改测评任务打分
     */
    @PreAuthorize("@ss.hasPermi('system:mark:edit')")
    @Log(title = "测评任务打分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskUserMark taskUserMark)
    {
        return toAjax(taskUserMarkService.updateTaskUserMark(taskUserMark));
    }

    /**
     * 删除测评任务打分
     */
    @PreAuthorize("@ss.hasPermi('system:mark:remove')")
    @Log(title = "测评任务打分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(taskUserMarkService.deleteTaskUserMarkByIds(ids));
    }
}
